let express = require('express');
let router = express.Router();

const questionService = require('../service/questionService');

// 引入jwt验证方法的中间件
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');
router.use(checkJWTMiddleWare);

// 获取公共课题目组
router.get('/getgonggongkeQuestionGroup', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await questionService.getgonggongkeQuestionGroup(uid);
    res.status(result.success ? 200 : 400).json(result);
});

// 获取专业课题目组
router.get('/getzhuanyekeQuestionGroup', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await questionService.getzhuanyekeQuestionGroup(uid);
    res.status(result.success ? 200 : 400).json(result);
});

// 获取指定公共课题目组
router.post('/getQuestionByGonggongke', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let gonggongke = req.body.gonggongke;
    if (!gonggongke) {
        return res.status(400).json({ success: false, msg: "科目错误" });
    }
    let result = await questionService.getQuestionByGonggongke(gonggongke, uid);
    res.status(result.success ? 200 : 400).json(result);
});

// 获取指定题库内容
router.post('/getQuestionContent', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let qid = req.body.qid;
    if (!qid) {
        return res.status(400).json({ success: false, msg: "参数错误" });
    }
    let result = await questionService.getQuestionContent(qid, uid);
    res.status(result.success ? 200 : 400).json(result);
});

// 获取所有题库
router.get('/getAllQuestion', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let page = parseInt(req.query.page || 1);
    let result = await questionService.getAllQuestion(uid, page);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;